package com.td.filemanager.ui;

import com.td.filemanager.ResourceTable;
import com.td.filemanager.utils.ResourceTool;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentState;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;

/**
 * 通用按钮元素
 */
public class GeneralButtonElement extends StateElement {
    private Context mContext;

    private int[] mStateFlagEmpty = new int[]{ComponentState.COMPONENT_STATE_EMPTY};

    private int[] mStateFlagPressed = new int[]{ComponentState.COMPONENT_STATE_PRESSED};

    private int[][] mAllTypeColorIds = {
            {
                    ResourceTable.Color_colorNormal, ResourceTable.Color_colorNormalPressed
            },
            {
                    ResourceTable.Color_colorAccent, ResourceTable.Color_colorAccentPressed
            },
            {
                    ResourceTable.Color_colorError, ResourceTable.Color_colorErrorPressed
            }
    };

    /**
     * 类型通常
     */
    public static final int TYPE_NORMAL = 0;

    /**
     * 类型突出
     */
    public static final int TYPE_ACCENT = 1;

    /**
     * 类型错误
     */
    public static final int TYPE_ERROR = 2;

    public GeneralButtonElement(Context context, int type) {
        super();
        mContext = context;
        initBackground(mAllTypeColorIds[type]);
    }

    private void initBackground(int[] colorIds) {
        this.addState(mStateFlagPressed, new ShapeElement() {
            {
                setRgbColor(RgbColor.fromArgbInt(ResourceTool.getColor(mContext, colorIds[1], 0)));
                setCornerRadius(ResourceTool.getFloat(mContext, ResourceTable.Float_dialog_corner_radius, 0));
            }
        });
        this.addState(mStateFlagEmpty, new ShapeElement() {
            {
                setRgbColor(RgbColor.fromArgbInt(ResourceTool.getColor(mContext, colorIds[0], 0)));
                setCornerRadius(ResourceTool.getFloat(mContext, ResourceTable.Float_dialog_corner_radius, 0));
            }
        });
    }
}
